/*
 * Created on Feb 11, 2005
 * Created by dfhuynh
 */
package edu.mit.simile.longwell.query.bucket;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.hp.hpl.jena.rdf.model.Model;

import edu.mit.simile.longwell.Profile;
import edu.mit.simile.longwell.ProfileListenerBase;
import edu.mit.simile.longwell.query.QueryException;

/**
 * @author dfhuynh
 */
public class BucketerManager extends ProfileListenerBase {
	final protected Profile	m_profile;
	final protected Map		m_bucketerNameToInstance = new HashMap();
	
	public BucketerManager(Profile profile) {
		m_profile = profile;
	}
	
	public IBucketer getBucketer(String name) throws QueryException {
		if (name == null) {
			name = DistinctValueBucketer.class.getName();
		}
		
		IBucketer bucketer = (IBucketer) 
			m_bucketerNameToInstance.get(name);
		
		if (bucketer == null) {
			try {
				Constructor constructor = 
					Class.forName(name).getConstructor(
						new Class[] { Profile.class });
				
				bucketer = (IBucketer) constructor.newInstance(
						new Object[] { m_profile });
				
				m_bucketerNameToInstance.put(name, bucketer);
			} catch (Exception e) {
				throw new QueryException(
					"Failed to create bucketer of name " + name, e);
			}
		}
		
		return bucketer;
	}
	
	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.ProfileListenerBase#onBeforeAdd(com.hp.hpl.jena.rdf.model.Model)
	 */
	public void onBeforeAdd(Model model) {
		Iterator i = m_bucketerNameToInstance.values().iterator();
		while (i.hasNext()) {
			IBucketer bucketer = (IBucketer) i.next();
			
			bucketer.onBeforeAdd(model);
		}
	}
	
	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.ProfileListenerBase#onAfterAdd(com.hp.hpl.jena.rdf.model.Model)
	 */
	public void onAfterAdd(Model model) {
		Iterator i = m_bucketerNameToInstance.values().iterator();
		while (i.hasNext()) {
			IBucketer bucketer = (IBucketer) i.next();
			
			bucketer.onAfterAdd(model);
		}
	}
	
	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.ProfileListenerBase#onFailingAdd(com.hp.hpl.jena.rdf.model.Model)
	 */
	public void onFailingAdd(Model model) {
		Iterator i = m_bucketerNameToInstance.values().iterator();
		while (i.hasNext()) {
			IBucketer bucketer = (IBucketer) i.next();
			
			bucketer.onFailingAdd(model);
		}
	}
	
	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.ProfileListenerBase#onBeforeRemove(com.hp.hpl.jena.rdf.model.Model)
	 */
	public void onBeforeRemove(Model model) {
		Iterator i = m_bucketerNameToInstance.values().iterator();
		while (i.hasNext()) {
			IBucketer bucketer = (IBucketer) i.next();
			
			bucketer.onBeforeRemove(model);
		}
	}
	
	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.ProfileListenerBase#onAfterRemove(com.hp.hpl.jena.rdf.model.Model)
	 */
	public void onAfterRemove(Model model) {
		Iterator i = m_bucketerNameToInstance.values().iterator();
		while (i.hasNext()) {
			IBucketer bucketer = (IBucketer) i.next();
			
			bucketer.onAfterRemove(model);
		}
	}
	
	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.ProfileListenerBase#onFailingRemove(com.hp.hpl.jena.rdf.model.Model)
	 */
	public void onFailingRemove(Model model) {
		Iterator i = m_bucketerNameToInstance.values().iterator();
		while (i.hasNext()) {
			IBucketer bucketer = (IBucketer) i.next();
			
			bucketer.onAfterAdd(model);
		}
	}
}
